When software is authored, it is often authored in a language that is more intuitive to a human software developer. Software code authored in this language is often termed “source code”. Source code cannot, however, be run directly by processors. Instead, the source code is compiled or interpreted into a machine-readable form often termed “machine code” or “binary”.
Compilers are now very sophisticated, and are even able to optimize the code prior to creating the machine code. Compilers generate the machine code using the source code as an input. However, the compilers also use information about the environment in transforming the source code into functionally equivalent machine code. During compilation, optimizations of the instruction flow may also be performed. For example, one instruction might be moved from one location to another if the functionality is not altered.
In performing these optimizations, conventional compilers take exception handling into account. As a program is executing, there may be times when the program encounters a state that the program itself is not able to handle. For example, the program might not be able to encounter a divide by zero operation, or perhaps an access violation, or a stack overflow condition. In those situations, the program will often “throw an exception”. The operating system then traps the exception, and finds the appropriate exception handler for that type of exception. The operating system then transfers control of execution back to the handler that accepted the exception.
Performing optimization of code that may throw an exception at runtime is problematic. After all, when an exception is thrown, code that is not within the program is run. Optimization of the program in the context of code that might run outside of the program is difficult because the program behavior becomes difficult to predict. Moving a line of code from one position to another may have unintended consequences when considering the possible execution of exception handling code outside of the program.